package com.vast.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vast.admin.entity.ContractRefund;
import com.vast.admin.service.impl.ContractRefundServiceImpl;
import com.vast.common.core.domain.AjaxResult;
import com.vast.common.core.domain.entity.SysUser;
import com.vast.common.utils.SecurityUtils;
import com.vast.common.constant.MessageCode;
import com.vast.common.core.domain.Result;
import com.vast.system.core.controller.BaseController;
import com.vast.system.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Api(value = "合同退款相关接口", tags = { "合同退款模块" })
@RestController
@RequestMapping("/manager/contract/contractRefund" )
public class ContractRefundController extends BaseController {

    @Autowired
    private ContractRefundServiceImpl iContractRefundService;


    /**
     * 查询合同退款列表
     */
    @GetMapping("/list")
    public Result<IPage<ContractRefund>> list(@ApiParam(hidden = true) Page<ContractRefund> page, ContractRefund contractRefund)
    {
        LambdaQueryWrapper<ContractRefund> lqw = Wrappers.lambdaQuery(contractRefund);
        if (contractRefund.getContractId() != null){
            lqw.eq(ContractRefund::getContractId ,contractRefund.getContractId());
        }
        if (StringUtils.isNotBlank(contractRefund.getContractNo())){
            lqw.like(ContractRefund::getContractNo ,contractRefund.getContractNo());
        }
        if (StringUtils.isNotBlank(contractRefund.getContractName())){
            lqw.like(ContractRefund::getContractName ,contractRefund.getContractName());
        }
        if (StringUtils.isNotBlank(contractRefund.getWechatNo())){
            lqw.eq(ContractRefund::getWechatNo ,contractRefund.getWechatNo());
        }
        if (StringUtils.isNotBlank(contractRefund.getStatus())){
            lqw.eq(ContractRefund::getStatus ,contractRefund.getStatus());
        }
        IPage<ContractRefund> list = iContractRefundService.page(page,lqw);
        return new Result<IPage<ContractRefund>>(MessageCode.SUCCESS, "获取成功", list);
    }

//    /**
//     * 导出合同退款列表
//     */
//    @Log(title = "合同退款" , businessType = BusinessType.EXPORT)
//    @GetMapping("/export" )
//    public AjaxResult export(ContractRefund contractRefund) {
//        LambdaQueryWrapper<ContractRefund> lqw = new LambdaQueryWrapper<ContractRefund>(contractRefund);
//        List<ContractRefund> list = iContractRefundService.list(lqw);
//        ExcelUtil util = new ExcelUtil(ContractRefund. class);
//        return util.exportExcel(list, "contractRefund" );
//    }

    /**
     * 获取合同退款详细信息
     */
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iContractRefundService.getById(id));
    }

    /**
     * 新增合同退款
     */
    @PostMapping
    public AjaxResult add(@RequestBody ContractRefund contractRefund) {
        return toAjax(iContractRefundService.save(contractRefund) ? 1 : 0);
    }

    /**
     * 修改合同退款
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ContractRefund contractRefund) {
        return toAjax(iContractRefundService.updateById(contractRefund) ? 1 : 0);
    }

    /**
     * 删除合同退款
     */
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iContractRefundService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 合同退款
     * @param contractRefund
     * @return
     */
    @PostMapping("/refund")
    public Result refund(ContractRefund contractRefund) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        iContractRefundService.refundApply(contractRefund, manager);
        return Result.SUCCESS;
    }

    /**
     * 审核
     * @param contractRefund
     * @return
     */
    @PostMapping("/approve")
    public Result approve(@RequestBody ContractRefund contractRefund) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        iContractRefundService.approveRefund(contractRefund, manager);
        return Result.SUCCESS;
    }

}
